//
//  ProductListsTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/4/9.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ProductListsTableViewCell.h"

@implementation ProductListsTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"ProductListsTableViewCell";
    // 1.缓存中取
    ProductListsTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 2.创建
    if (cell == nil) {
        cell = [[ProductListsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setCategoryModel:(ProdcutListSubCategoryModel *)categoryModel{
    if (!_viewsArray) {
        _viewsArray=[[NSMutableArray alloc] init];
    }
    for (SingleProductView *subView in _viewsArray) {
        [subView removeFromSuperview];
    }
    _categoryModel=categoryModel;
    for (int i=0; i<categoryModel.products.count; i++) {
        ProductListDetailModel *detailModel=categoryModel.products[i];
        SingleProductView *productView=[[SingleProductView alloc] initWithFrame:CGRectMake((i%2)*SCREEN_WIDTH/2, (i/2)*240,SCREEN_WIDTH/2, 240)];
        [productView setProductModel:detailModel];
        [productView addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        productView.tag=i;
        [_viewsArray addObject:productView];
        [self addSubview:productView];
    }
}

-(void)setProductsModel:(Product_listModel *)productsModel{
    if (!_viewsArray) {
        _viewsArray=[[NSMutableArray alloc] init];
    }
    for (SingleProductView *subView in _viewsArray) {
        [subView removeFromSuperview];
    }
    _productsModel=productsModel;
    for (int i=0; i<productsModel.module_data.products.count; i++) {
        ProductDetailModel *detailModel=productsModel.module_data.products[i];
        SingleProductView *productView=[[SingleProductView alloc] initWithFrame:CGRectMake((i%2)*SCREEN_WIDTH/2, (i/2)*240,SCREEN_WIDTH/2, 240)];
        [productView setProductModel:detailModel];
        [productView addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        productView.tag=i;
        [_viewsArray addObject:productView];
        [self addSubview:productView];
    }
}
-(void)setSearchModel:(SearchResultModel *)searchModel{
    if (!_viewsArray) {
        _viewsArray=[[NSMutableArray alloc] init];
    }
    for (SingleProductView *subView in _viewsArray) {
        [subView removeFromSuperview];
    }
    _searchModel=searchModel;
    for (int i=0; i<searchModel.data.products.count; i++) {
        SingleProductModel *detailModel=searchModel.data.products[i];
        SingleProductView *productView=[[SingleProductView alloc] initWithFrame:CGRectMake((i%2)*SCREEN_WIDTH/2, (i/2)*240,SCREEN_WIDTH/2, 240)];
        [productView setProductModel:detailModel];
        [productView addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        productView.tag=i;
        [_viewsArray addObject:productView];
        [self addSubview:productView];
    }

}
-(void)setCollectionModel:(CollectionSubModel *)collectionModel{
    if (!_viewsArray) {
        _viewsArray=[[NSMutableArray alloc] init];
    }
    for (SingleProductView *subView in _viewsArray) {
        [subView removeFromSuperview];
    }

    _collectionModel=collectionModel;
    if (collectionModel.products.count>0) {
        for (int i=0; i<collectionModel.products.count; i++) {
            ProductListDetailModel *detailModel=collectionModel.products[i];
            SingleProductView *productView=[[SingleProductView alloc] initWithFrame:CGRectMake((i%2)*SCREEN_WIDTH/2, (i/2)*240,SCREEN_WIDTH/2, 240)];
            [productView setProductModel:detailModel];
            [productView addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            productView.tag=i;
            [_viewsArray addObject:productView];
            [self addSubview:productView];
        }
 
    }
    if (collectionModel.recommend.count>0) {
        
        for (int i=0; i<collectionModel.recommend.count; i++) {
            ProductListDetailModel *detailModel=collectionModel.recommend[i];
            SingleProductView *productView=[[SingleProductView alloc] initWithFrame:CGRectMake((i%2)*SCREEN_WIDTH/2, (i/2)*240,SCREEN_WIDTH/2, 240)];
            [productView setProductModel:detailModel];
            [productView addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            productView.tag=i;
            [_viewsArray addObject:productView];
            [self addSubview:productView];
        }

    }
    
}
-(void)buttonAction:(UIButton*)bt{
    if (_productsModel) {
        ProductsModel *productsModel=_productsModel.module_data;
        ProductDetailModel *detailModel=productsModel.products[bt.tag];
        [TransferManager transferToSomeViewType:@"product" linkID:detailModel.product_id  linkName:@"" fromView:self];
    }
    if (_categoryModel) {
        ProductListDetailModel *detailModel=_categoryModel.products[bt.tag];
        [TransferManager transferToSomeViewType:@"product" linkID:detailModel.product_id linkName:@"" fromView:self];
    }
    if (_searchModel) {
        SingleProductModel *detailModel=_searchModel.data.products[bt.tag];
        [TransferManager transferToSomeViewType:@"product" linkID:detailModel.product_id linkName:@"" fromView:self];
    }
    if (_collectionModel) {
        if (_collectionModel.products.count>0) {
            ProductListDetailModel *detailModel=_collectionModel.products[bt.tag];
            [TransferManager transferToSomeViewType:@"product" linkID:detailModel.product_id linkName:@"" fromView:self];
        }
        if (_collectionModel.recommend.count>0) {
            ProductListDetailModel *detailModel=_collectionModel.recommend[bt.tag];
            [TransferManager transferToSomeViewType:@"product" linkID:detailModel.product_id linkName:@"" fromView:self];
        }
      
    }

}
@end
